package idiet.dao.hibernate;

import idiet.bean.MealRecipe;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository("mealRecipeDao")
public class MealRecipeDao extends BaseDao {
	public MealRecipe getMealRecipById(int mealRecipeId){
		return (MealRecipe) this.sessionFactory
				.getCurrentSession()
				.createCriteria(MealRecipe.class)
				.add(Restrictions.eq("id", mealRecipeId))
				.uniqueResult();
	}
	@SuppressWarnings("unchecked")
	public List<MealRecipe> getMealRecipeByMealRecipeName(String name){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(MealRecipe.class)
				.add(Restrictions.like("name",name))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<MealRecipe> getMealRecipe(){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(MealRecipe.class)
				.list();
	}
	public void createMealRecipe(MealRecipe mealRecipe){
		this.sessionFactory
		.getCurrentSession()
		.save(mealRecipe);
	}
	public void createMealRecipe(List<MealRecipe> mealRecipes){
		Session session=this.sessionFactory.getCurrentSession();
		//session.beginTransaction();
		for(MealRecipe mr:mealRecipes){
			session.save(mr);
		}
		//session.beginTransaction().commit();
		//session.close();
		
	}
	public void updateMealRecipe(MealRecipe mealRecipe){
		this.sessionFactory
		.getCurrentSession()
		.update(mealRecipe);
	}
	public void deleteMealRecipe(MealRecipe mealRecipe){
		this.sessionFactory
		.getCurrentSession()
		.delete(mealRecipe);
	}
}
